package cz.jm.web.manhunter.client.common;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;


public class FormPanel implements Panel {
	/** Formular - jadro. */
	private final DynamicForm form = new DynamicForm();
	/** Potvrdenie formulara. */
	private final ButtonItem submitButton = new ButtonItem();
	
	

	
	public FormPanel() {	
		form.setAutoHeight();
		form.setAutoWidth();
		form.setSaveOnEnter(true);
		form.setPadding(10);
		
		submitButton.setWidth(100);
		submitButton.setColSpan(2);
		submitButton.setAlign(Alignment.RIGHT);

		submitButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				form.submit();
			}
		});
		
	}

	public DynamicForm getForm() {
    	return form;
    }

	public ButtonItem getSubmitButton() {
    	return submitButton;
    }
	
	public void setSubmitable(boolean submitable) {
		form.setSaveOnEnter(submitable);
		submitButton.setVisible(submitable);
	}
	
	public void setFields(FormItem... items) {
		List<FormItem> fields = new LinkedList<FormItem>(Arrays.asList(items));
		fields.add(submitButton);
		form.setFields(fields.toArray(new FormItem[1]));
	}

	@Override
    public Canvas asCanvas() {
	    return form;
    }

}
